package com.remembercard;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class Remember extends Activity {
	
	private int[][] cards;
	private Memory myMem;
	private int num = 10;
	private int count;
	
	private TextView title;
	private ImageView card;
	private Button button;
	private TextView askValue;
	private TextView askSuit;
	private EditText getValue;
	private EditText getSuit;
	private TextView message;

	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initialize();
        myMem = new Memory(num);
        initGame();
    }
    
    private void initGame() {
    	button.setText("Start");
    	message.setText("Start game");
    	button.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				button.setText("Next");
				Card next = myMem.getNext();
				if(next != null) {
					card.setImageResource(resolveImg(next.getCardId()));
					message.setText("Remember the number: " + String.valueOf(count + 1));
					count++;
				} else {
					card.setImageResource(R.drawable.smile);
					count = 0;
					startGuess();
				}
			}
    	});
    }
    
    private void startGuess() {
    	count = 0;
    	button.setText("Guess");
    	message.setText("Start Guess");
    	button.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				if(count == 0) {
					askValue.setVisibility(TextView.VISIBLE);
			    	askSuit.setVisibility(TextView.VISIBLE);
			    	getValue.setVisibility(EditText.VISIBLE);
			    	getSuit.setVisibility(EditText.VISIBLE);
			    	message.setText("Guess the number" + String.valueOf(count + 1));
			    	count++;
				} else if(count < num) {
					String chosen = getSuit.getText().charAt(0) + getValue.getText().toString();
					message.setText("Guess the number" + String.valueOf(count + 1));					
					myMem.setPoints(chosen, count - 1);
					count++;
				} else if(count == num) {
					String chosen = getSuit.getText().charAt(0) + getValue.getText().toString();
					myMem.setPoints(chosen, count - 1);
					count++;
					askValue.setVisibility(TextView.INVISIBLE);
			    	askSuit.setVisibility(TextView.INVISIBLE);
			    	getValue.setVisibility(EditText.INVISIBLE);
			    	getSuit.setVisibility(EditText.INVISIBLE);
					message.setText("Your score: " + myMem.getResult());
					button.setClickable(false);
					button.setText("Game finished");
				}
			}
    	});
    }
    
    private void initialize() {
    	title = (TextView) findViewById(R.id.title);
    	title.setText("Remember the cards");
    	card = (ImageView) findViewById(R.id.image);
    	card.setImageResource(R.drawable.smile);
    	button = (Button) findViewById(R.id.button);
    	askValue = (TextView) findViewById(R.id.number);
    	askValue.setText("Value");
    	askValue.setVisibility(TextView.INVISIBLE);
    	askSuit = (TextView) findViewById(R.id.suit);
    	askSuit.setText("Suit");
    	askSuit.setVisibility(TextView.INVISIBLE);
    	getValue = (EditText) findViewById(R.id.numberIn);
    	getValue.setEnabled(true);
    	getValue.setVisibility(EditText.INVISIBLE);
    	getSuit = (EditText) findViewById(R.id.suitIn);
    	getSuit.setEnabled(true);
    	getSuit.setVisibility(EditText.INVISIBLE);
    	message = (TextView) findViewById(R.id.messages);
    	cards = new int[4][13];
    	cards[0][0] = R.drawable.c1;
    	cards[0][1] = R.drawable.c2;
    	cards[0][2] = R.drawable.c3;
    	cards[0][3] = R.drawable.c4;
    	cards[0][4] = R.drawable.c5;
    	cards[0][5] = R.drawable.c6;
    	cards[0][6] = R.drawable.c7;
    	cards[0][7] = R.drawable.c8;
    	cards[0][8] = R.drawable.c9;
    	cards[0][9] = R.drawable.c10;
    	cards[0][10] = R.drawable.c11;
    	cards[0][11] = R.drawable.c12;
    	cards[0][12] = R.drawable.c13;
    	
    	cards[1][0] = R.drawable.d1;
    	cards[1][1] = R.drawable.d2;
    	cards[1][2] = R.drawable.d3;
    	cards[1][3] = R.drawable.d4;
    	cards[1][4] = R.drawable.d5;
    	cards[1][5] = R.drawable.d6;
    	cards[1][6] = R.drawable.d7;
    	cards[1][7] = R.drawable.d8;
    	cards[1][8] = R.drawable.d9;
    	cards[1][9] = R.drawable.d10;
    	cards[1][10] = R.drawable.d11;
    	cards[1][11] = R.drawable.d12;
    	cards[1][12] = R.drawable.d13;
    	
    	cards[2][0] = R.drawable.p1;
    	cards[2][1] = R.drawable.p2;
    	cards[2][2] = R.drawable.p3;
    	cards[2][3] = R.drawable.p4;
    	cards[2][4] = R.drawable.p5;
    	cards[2][5] = R.drawable.p6;
    	cards[2][6] = R.drawable.p7;
    	cards[2][7] = R.drawable.p8;
    	cards[2][8] = R.drawable.p9;
    	cards[2][9] = R.drawable.p10;
    	cards[2][10] = R.drawable.p11;
    	cards[2][11] = R.drawable.p12;
    	cards[2][12] = R.drawable.p13;
    
    	cards[3][0] = R.drawable.t1;
    	cards[3][1] = R.drawable.t2;
    	cards[3][2] = R.drawable.t3;
    	cards[3][3] = R.drawable.t4;
    	cards[3][4] = R.drawable.t5;
    	cards[3][5] = R.drawable.t6;
    	cards[3][6] = R.drawable.t7;
    	cards[3][7] = R.drawable.t8;
    	cards[3][8] = R.drawable.t9;
    	cards[3][9] = R.drawable.t10;
    	cards[3][10] = R.drawable.t11;
    	cards[3][11] = R.drawable.t12;
    	cards[3][12] = R.drawable.t13;
    }
    
    private int resolveImg(String cardId) {
    	if(cardId.length() != 2 && cardId.length() != 3) {
    		return 0;
    	}
    	char auxSuit = cardId.charAt(0);
    	if(auxSuit != 'c' && auxSuit != 'd' && auxSuit != 'p' && auxSuit != 't') {
    		return 0;
    	}
    	int value = cardId.charAt(1) - '0';
    	if(value < 0 || value > 9) {
    		return 0;
    	}
    	if(cardId.length() == 3) {
    		int aux = cardId.charAt(2) - '0';
    		if(aux < 0 || aux > 9) {
    			return 0;
    		}
    		value = value * 10 + aux;
    	}
    	if(value > 13) {
    		return 0;
    	}
    	int suit = 0;
    	if(auxSuit == 'd') {
    		suit = 1;
    	} else if (auxSuit == 'p'){
    		suit = 2;
    	} else if (auxSuit == 't') {
    		suit = 3;
    	}
    	return cards[suit][value - 1];
    }
}